/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.collection.CollUtil
 *  cn.hutool.core.util.ObjectUtil
 *  com.alibaba.fastjson.JSON
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.collections.FXCollections
 *  javafx.collections.ObservableList
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.Node
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.CheckBox
 *  javafx.scene.control.TableCell
 *  javafx.scene.control.TableColumn
 *  javafx.scene.control.TableView
 *  javafx.scene.control.cell.PropertyValueFactory
 *  javafx.scene.layout.AnchorPane
 *  javafx.util.Callback
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.util.CollectionUtils
 */
package com.gys.controller.pay;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.gys.common.business.PayUtil;
import com.gys.common.data.HttpJson;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.pay.PayController;
import com.gys.controller.salesReceipts.SalesReceiptsMainController;
import com.gys.data.pay.GetDzqInData;
import com.gys.data.pay.GetDzqOutData;
import com.gys.data.pay.GetPayInData;
import de.felixroske.jfxsupport.FXMLController;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.util.Callback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

@FXMLController
public class DzqController
extends BaseController
implements Initializable {
    @FXML
    private AnchorPane pane;
    @FXML
    private TableView<GetDzqOutData> tableView;
    @FXML
    private TableColumn<GetDzqOutData, String> gspcbActNo;
    @FXML
    private TableColumn<GetDzqOutData, String> gspcbName;
    @FXML
    private TableColumn<GetDzqOutData, String> gspcbCouponAmt;
    @FXML
    private TableColumn<GetDzqOutData, CheckBox> checkBox;
    @FXML
    private TableColumn<GetDzqOutData, String> gspcbDesc;
    @FXML
    private TableColumn<GetDzqOutData, String> gspcbEndDate;
    @FXML
    private TableColumn<GetDzqOutData, String> gspcbCount;
    @Autowired
    private PayController payController;
    @Autowired
    private SalesReceiptsMainController salesReceiptsMainController;
    private ObservableList<GetDzqOutData> observableTableList = FXCollections.observableArrayList(new ArrayList());

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        this.init();
    }

    @Override
    public void init() {
        if (ObjectUtil.isEmpty(this.pane)) {
            return;
        }
        GetPayInData payInData = new GetPayInData();
        payInData.setBillNo(this.salesReceiptsMainController.getCaseId().getText());
        payInData.setCardNo(this.salesReceiptsMainController.getCardNum().getText());
        payInData.setAmount(new BigDecimal(this.payController.getYsAmount().getText()));
        payInData.setCount(this.salesReceiptsMainController.getObservableTableList().size() + "");
        HttpJson httpJson = PayUtil.dzqList(payInData);
        List dataList = new ArrayList();
        if (httpJson.isSuccess()) {
            dataList = JSON.parseArray((String)httpJson.getMsg(), GetDzqOutData.class);
            this.observableTableList = FXCollections.observableArrayList(dataList);
            for (int i = 0; i < this.observableTableList.size(); ++i) {
                GetDzqOutData dzqOutData = (GetDzqOutData)this.observableTableList.get(i);
                if ("1".equals(dzqOutData.getGspcbName())) {
                    dzqOutData.setGspcbName("\u901a\u7528\u5238");
                    continue;
                }
                dzqOutData.setGspcbName("\u54c1\u7c7b\u5238");
            }
        } else {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", httpJson.getMsg());
            alert.showAndWait();
            return;
        }
        this.initTable();
    }

    public TableView<GetDzqOutData> initTable() {
        this.bingTableCell();
        this.tableView.setItems(this.observableTableList);
        return this.tableView;
    }

    private void bingTableCell() {
        this.checkBox.setCellValueFactory(cellData -> ((GetDzqOutData)cellData.getValue()).getCheckBox().getCustomCheckBox());
        this.checkBox.setCellFactory(col -> {
            TableCell<GetDzqOutData, CheckBox> cell = new TableCell<GetDzqOutData, CheckBox>(){

                protected void updateItem(CheckBox item, boolean empty) {
                    super.updateItem(item, empty);
                    this.setGraphic((Node)item);
                    if (item != null) {
                        item.setOnMouseClicked(col -> {
                            if (CollUtil.isEmpty((Collection)DzqController.this.observableTableList)) {
                                return;
                            }
                        });
                    }
                }
            };
            return cell;
        });
        this.gspcbActNo.setCellValueFactory((Callback)new PropertyValueFactory(this.gspcbActNo.getId()));
        this.gspcbName.setCellValueFactory((Callback)new PropertyValueFactory(this.gspcbName.getId()));
        this.gspcbCouponAmt.setCellValueFactory((Callback)new PropertyValueFactory(this.gspcbCouponAmt.getId()));
        this.gspcbDesc.setCellValueFactory((Callback)new PropertyValueFactory(this.gspcbDesc.getId()));
        this.gspcbEndDate.setCellValueFactory((Callback)new PropertyValueFactory(this.gspcbEndDate.getId()));
        this.gspcbCount.setCellValueFactory((Callback)new PropertyValueFactory(this.gspcbCount.getId()));
    }

    private List<GetDzqOutData> getSelectIndex() {
        ArrayList<GetDzqOutData> ids = new ArrayList<GetDzqOutData>();
        if (!CollectionUtils.isEmpty(this.observableTableList)) {
            for (GetDzqOutData i : this.observableTableList) {
                if (!i.getCheckBox().isSelected().booleanValue()) continue;
                ids.add(i);
            }
        }
        return ids;
    }

    @FXML
    private void sureAction() {
        List<GetDzqOutData> list = this.getSelectIndex();
        BigDecimal dzqAmount = BigDecimal.ZERO;
        ArrayList<GetDzqInData> dzqList = new ArrayList<GetDzqInData>();
        for (GetDzqOutData outData : list) {
            dzqAmount = dzqAmount.add(outData.getGspcbCouponAmt().multiply(new BigDecimal(outData.getGspcbCount())));
            GetDzqInData inData = new GetDzqInData();
            inData.setProCode(outData.getProCode());
            inData.setGspcbActNo(outData.getGspcbActNo());
            inData.setGspcbCount(outData.getGspcbCount());
            inData.setGspcbCouponAmt(outData.getGspcbCouponAmt());
            dzqList.add(inData);
        }
        if (dzqAmount.compareTo(new BigDecimal(this.payController.getDsAmount().getText())) > 0) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u7535\u5b50\u5238\u91d1\u989d\u4e0d\u80fd\u5927\u4e8e\u5f85\u4ed8\u91d1\u989d");
            alert.showAndWait();
            return;
        }
        this.payController.refreshAmount("9002", dzqAmount.setScale(2, 4).toString());
        this.payController.setDzqAmount(dzqAmount.setScale(2, 4).toString());
        this.payController.setDzqList(dzqList);
        this.pane.getScene().getWindow().hide();
    }

    @FXML
    private void cancelAction() {
        this.pane.getScene().getWindow().hide();
    }
}

